﻿using System;
using System.Data;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using log4net;
namespace AnyOffice.general.qikan
{
    public partial class qikan : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.Page.IsPostBack)
            {
                string str = (Request.QueryString["path"] != null) ? Request.QueryString["path"].ToString() : "/";
                this.ViewState["CurrentPath"] = str;
                QikanInfo qikanInfo = QikanFactory.GetQikanInfo();
                if (qikanInfo == null)
                {
                    Response.Redirect("index.aspx");
                }
                else
                {
                    this.litNavTitle.Text = qikanInfo.Name;
                    bool canNew = qikanInfo.CanNew;
                    bool canDownload = qikanInfo.CanDownload;
                    bool canManage = qikanInfo.CanManage;
                    if (!canNew && !canManage)
                    {
                        this.btnNewFile.Visible = false;
                    }
                    if (!canNew && !canManage)
                    {
                        this.btnNewFloder.Visible = false;
                        this.btnNewFile.Visible = false;
                    }
                    if (!canManage)
                    {
                        this.btnFolderAttribute.Visible = false;
                        this.btnDelete.Visible = false;
                        this.btnMove.Visible = false;
                        this.destFolder.Visible = false;
                        this.checkAll.Visible = false;
                    }
                    this.ViewState["CanNew"] = canNew;
                    this.ViewState["CanDownload"] = canDownload;
                    this.ViewState["CanManage"] = canManage;
                    this.ViewState["CanRead"] = qikanInfo.CanRead;
                    this.ViewState["CanEdit"] = qikanInfo.CanEdit;
                    this.GetFileInfo(str);
                    this.btnDelete.Attributes.Add("onclick", "return delete_confirm();");
                    this.btnMove.Attributes.Add("onclick", "return move_confirm();");
                }
            }
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            QikanInfo qikanInfo = QikanFactory.GetQikanInfo();
            if (qikanInfo.CanManage)
            {
                try
                {
                    string diskPath = qikanInfo.DiskPath;
                    string str2 = "";
                    foreach (RepeaterItem item in this.rptInfo.Items)
                    {
                        HtmlInputCheckBox box = (HtmlInputCheckBox)item.FindControl("PathId");
                        if ((box != null) && box.Checked)
                        {
                            str2 = box.Value.Replace("../", "").Replace("/", @"\");
                            if (str2.StartsWith("DIR"))
                            {
                                str2 = str2.Substring(3, str2.Length - 3);
                                new DirectoryInfo(diskPath + str2).Delete(true);
                            }
                            if (str2.StartsWith("FILE"))
                            {
                                str2 = str2.Substring(4, str2.Length - 4);
                                new FileInfo(diskPath + str2).Delete();
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
                this.checkAll.Checked = false;
                this.GetFileInfo(this.ViewState["CurrentPath"].ToString());
            }
        }

        protected void btnFolderAttribute_Click(object sender, EventArgs e)
        {
            Response.Redirect("changefolder.aspx?foldername=" + Server.UrlEncode(this.currentFolder.SelectedValue));
        }

        protected void btnMove_Click(object sender, EventArgs e)
        {
            QikanInfo qikanInfo = QikanFactory.GetQikanInfo();
            if (qikanInfo.CanManage)
            {
                try
                {
                    string diskPath = qikanInfo.DiskPath;
                    string str2 = (qikanInfo.DiskPath + this.destFolder.SelectedValue).Replace("/", @"\");
                    if (str2.EndsWith(@"\"))
                    {
                        str2 = str2.Substring(0, str2.Length - 1);
                    }
                    string str3 = "";
                    foreach (RepeaterItem item in this.rptInfo.Items)
                    {
                        HtmlInputCheckBox box = (HtmlInputCheckBox)item.FindControl("PathId");
                        if ((box != null) && box.Checked)
                        {
                            str3 = box.Value.Replace("../", "").Replace("/", @"\");
                            if (str3.StartsWith("DIR"))
                            {
                                str3 = str3.Substring(3, str3.Length - 3);
                                DirectoryInfo info2 = new DirectoryInfo(diskPath + str3);
                                Response.Write(str2 + str3);
                                if (str2.StartsWith(info2.FullName))
                                {
                                    this.pnlMain.Visible = false;
                                    this.litMessage.Text = base.ShowError("不允许将一个目录移动到它的子目录下面！", "qikan.aspx?path=" + Server.UrlEncode(this.ViewState["CurrentPath"].ToString()));
                                    return;
                                }
                                if (info2.Parent.FullName != str2)
                                {
                                    if (Directory.Exists(str2 + @"\" + info2.Name))
                                    {
                                        this.pnlMain.Visible = false;
                                        this.litMessage.Text = base.ShowError("移动操作出错：已有同名目录存在！", "qikan.aspx?path=" + Server.UrlEncode(this.ViewState["CurrentPath"].ToString()));
                                        return;
                                    }
                                    try
                                    {
                                        info2.MoveTo(str2 + @"\" + info2.Name);
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                            }
                            if (str3.StartsWith("FILE"))
                            {
                                str3 = str3.Substring(4, str3.Length - 4);
                                FileInfo info3 = new FileInfo(diskPath + str3);
                                if (info3.Directory.FullName == str2)
                                {
                                    continue;
                                }
                                if (File.Exists(str2 + @"\" + info3.Name))
                                {
                                    this.pnlMain.Visible = false;
                                    this.litMessage.Text = base.ShowError("移动操作出错：已有同名文件存在！", "qikan.aspx?path=" + Server.UrlEncode(this.ViewState["CurrentPath"].ToString()));
                                    return;
                                }
                                try
                                {
                                    info3.MoveTo(str2 + @"\" + info3.Name);
                                    continue;
                                }
                                catch (Exception)
                                {
                                    continue;
                                }
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
                this.checkAll.Checked = false;
                this.GetFileInfo(this.ViewState["CurrentPath"].ToString());
            }
        }

        protected void btnNewFile_Click(object sender, EventArgs e)
        {
            Response.Redirect("newfile.aspx?parent_folder=" + Server.UrlEncode(this.currentFolder.SelectedValue));
        }

        protected void btnNewFloder_Click(object sender, EventArgs e)
        {
            Response.Redirect("newfolder.aspx?parent_folder=" + Server.UrlEncode(this.currentFolder.SelectedValue));
        }

        protected void currentFolder_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("qikan.aspx?path=" + Server.UrlEncode(this.currentFolder.SelectedValue));
        }

        public string formatSize(long size)
        {
            if ((size > 0L) && (size < 1024L))
            {
                return "1k";
            }
            if (size < 1048576L)
            {
                return (Convert.ToString((long)(size / 1024L)) + "k");
            }
            float num = Convert.ToSingle(size) / 1048576f;
            return (num.ToString("0.0") + "M");
        }
        public string OfficeMenu(string path)
        {
            string str = string.Empty;
            if (((path.ToLower().EndsWith("doc") || path.ToLower().EndsWith("xls")) || (path.ToLower().EndsWith("ppt") || path.ToLower().EndsWith("docx"))) || (path.ToLower().EndsWith("xlsx") || path.ToLower().EndsWith("pptx")))
            {
                if (Convert.ToBoolean(this.ViewState["CanRead"].ToString()))
                {
                    str = "<input type=\"button\" value=\"阅读\" class=\"SmallButton\" onClick=\"window.open('read.aspx?filename=" + Server.UrlEncode(path) + "',null,'menubar=0,toolbar=0,status=1,scrollbars=1,resizable=1');\"> ";
                }
                if (Convert.ToBoolean(this.ViewState["CanEdit"].ToString()))
                {
                    str = str + "<input type=\"button\" value=\"编辑\" class=\"SmallButton\" onClick=\"window.open('edit.aspx?filename=" + Server.UrlEncode(path) + "',null,'menubar=0,toolbar=0,status=1,scrollbars=1,resizable=1');\">";
                }
            }
            return str;
        }


        private void GetFileInfo(string currentPath)
        {
            string str = "/";
            if (currentPath.LastIndexOf("/") > 0)
            {
                str = currentPath.Substring(0, currentPath.LastIndexOf("/"));
            }
            this.ViewState["ParentPath"] = str;
            if (currentPath != "/")
            {
                this.pnlJumpParent.Visible = true;
            }
            DirectoryUtility utility = new DirectoryUtility(currentPath);
            this.rptInfo.DataSource = utility.DtbInfo;
            this.rptInfo.DataBind();
            DataTable dtbDirectory = new LiteUtility().GetDtbDirectory();
            this.currentFolder.DataSource = dtbDirectory;
            this.currentFolder.DataTextField = "path";
            this.currentFolder.DataValueField = "path";
            this.currentFolder.DataBind();
            if (this.currentFolder.Items.FindByValue(currentPath) != null)
            {
                this.currentFolder.Items.FindByValue(currentPath).Selected = true;
            }
            this.destFolder.DataSource = dtbDirectory;
            this.destFolder.DataTextField = "path";
            this.destFolder.DataValueField = "path";
            this.destFolder.DataBind();
            this.litStatic.Text = "共 " + utility.FileCount.ToString() + " 个文件, " + this.formatSize(utility.TotalSize);
        }


        public string Thumb(string path, string upDownMaximum)
        {
            string str = string.Empty;
            if ((!path.ToLower().EndsWith("gif") && !path.ToLower().EndsWith("jpg")) && ((!path.ToLower().EndsWith("jpeg") && !path.ToLower().EndsWith("bmp")) && !path.ToLower().EndsWith("png")))
            {
                return str;
            }
            str = "<a target=_blank href=show_photo.aspx?filename=" + Server.UrlEncode(path) + "><img align=absMiddle border=0 src=thumb.aspx?filename=" + Server.UrlEncode(path);
            if (upDownMaximum != string.Empty)
            {
                str = str + "&upDownMaximum=" + upDownMaximum;
            }
            return (str + "></a>");
        }
    }
}
